package com.algorithm.string_and_array;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class SummarizeSolutionLC03 {
    public String reverseWords(String s) {
        // 1 先利用双指针倒叙
        char[] chars = s.toCharArray();
        int k = 0;
        int h = chars.length-1;
        while (k<h){
            char tmp = chars[k];
            chars[k] = chars[h];
            chars[h] = tmp;
            k++;
            h--;
        }

        s = new String(chars);
        String[] split = s.split(" ");
        k = 0;
        h = split.length-1;
        while (k< h){
            String tmp = split[k];
            split[k] = split[h];
            split[h] = tmp;
            k++;
            h--;
        }
        String result = "";
        for (int i = 0; i < split.length ; i++) {
            result = result+split[i]+" ";
        }
        return result.trim();
    }

    @Test
    public void test(){
        String s = "Let's take LeetCode contest";
        String s1 = reverseWords(s);
        log.info("this is test");
    }


    public String reverseWords1(String s) {
        // 1 先利用双指针倒叙
        // 1 先利用双指针倒叙
        String[] split = s.split(" ");
        String result = "";
        for (int i = 0; i < split.length ; i++) {
            char[] chars = split[i].toCharArray();
            int k = 0;
            int h = chars.length-1;
            while (k<h){
                char tmp = chars[k];
                chars[k] = chars[h];
                chars[h] = tmp;
                k++;
                h--;
            }
            result = result+ new String(chars)+" ";
        }
        return result.trim();
    }

    @Test
    public void test1(){
        String s = "Let's take LeetCode contest";
        String s1 = reverseWords1(s);
        log.info("this is test");
    }
}
